﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BandAid.DalCE;

namespace BandAid
{
    public class ProcessLogic
    {

        #region singleton
        private ProcessLogic()
        { }

        private static ProcessLogic _instance = null;

        public static ProcessLogic Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ProcessLogic();

                return _instance;
            }
        }
        #endregion


        public DalCE.BandAid dc = new DalCE.BandAid(Properties.Settings.Default.BandAidConnectionStringFile);

        public void UpdateDal()
        {
            dc.SubmitChanges();
        }

        /// <summary>
        /// Returns all the songs of a given artist
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public List<SONGS> GetSongsByArtist(ARTISTS a)
        {
            var r = from s in ProcessLogic.Instance.dc.SONGS where s.ARTIST_ID == a.ID select s;

            List<SONGS> res = r.ToList<SONGS>();

            return res;
        }

        public List<SHOW_SONGS> GetShowsWhereASongHasBeenPerformed(SONGS s)
        {

            var r = from show in ProcessLogic.Instance.dc.SHOW_SONGS where show.SONG_ID == s.ID select show;

            List<SHOW_SONGS> res = r.ToList<SHOW_SONGS>();

            return res;
        }

        public void DeleteSong(SONGS s)
        {
            List<SHOW_SONGS> shows = this.GetShowsWhereASongHasBeenPerformed(s);

            if (shows.Count > 0)
            {
                 throw new Exception(string.Format("The song you're trying delete ({0}) has been performed in shows, so you can't delete it (and its artist) unless you delete the show first", s.ToString()));
               // MessageBox.Show(string.Format("The song you're trying delete ({0}) has been performed in shows, so you can't delete it (and its artist) unless you delete the show first", s.ToString()));
                
            }
            else
            {
                ProcessLogic.Instance.dc.SONGS.DeleteOnSubmit(s);
                ProcessLogic.Instance.UpdateDal();
            }
        }

        /// <summary>
        /// Retrieves a song by its id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SONGS GetSongById(int id)
        {
            var r = from song in ProcessLogic.Instance.dc.SONGS where song.ID == id select song;


            if (r.Count() == 1)
            {
                return r.Single<SONGS>();
            }
            else
                return null;
        }

        public void DeleteArtist(ARTISTS a)
        {
            List<SONGS> songs = ProcessLogic.Instance.GetSongsByArtist(a);

            if (songs != null && songs.Count > 0)
            {
                foreach (SONGS s in songs)
                {
                    try
                    {
                        ProcessLogic.Instance.DeleteSong(s);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show(string.Format("The song you're trying delete ({0}) has been performed in shows, so you can't delete it (and its artist) unless you delete the show first", s.ToString()));
                        return;
                    }

                }
            }


            ProcessLogic.Instance.dc.ARTISTS.DeleteOnSubmit(a);
            ProcessLogic.Instance.UpdateDal();
        }
    }
}
